<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class Router extends Model {
		/*
		$authenController = array(
			array( "controller" => "controllerName" , "action" => array( "index" , "action" ),
			array( "controller" => "controllerName" , "action" => array( "index" , "action" ),
		);
		*/
		private $request = array();
		private $authenController = array();
		private $controler = array( "controller" => "page" , "action" => "index");
		
		public function __construct(){}
		public function Rounter(){
			$this->__construct();
		}
		public function setAuthenController( $controller = array() ){
			if ( $this->checkControllerFormat( $controller ) ){
				$this->authenController[] = $controller ;
				return true ;
			}
			return false;
		}
		private function getAndClearData(){
			$this->request["GET"] = $_GET;
			$this->request["REQUEST"] = $_REQUEST;
			$this->request["POST"] = $_POST;
			//$_GET=$_POST=$_REQUEST=array();
			isset($this->request["REQUEST"]["com"])&&isset($this->request["REQUEST"]["com"])!=""?$this->controler["controller"]=$this->request["REQUEST"]["com"]:false;
			isset($this->request["REQUEST"]["action"])&&isset($this->request["REQUEST"]["action"])!=""?$this->controler["action"]=$this->request["REQUEST"]["action"]:false;
			unset($this->request["REQUEST"]["com"]);
			unset($this->request["REQUEST"]["action"]);
			unset($this->request["GET"]["com"]);
			unset($this->request["GET"]["action"]);
			unset($this->request["POST"]["com"]);
			unset($this->request["POST"]["action"]);
		}
		public function route( $controller = array() ){
			$this->getAndClearData();
			$this->validateRoutes();
			$controller==array()?$controller=$this->controler:false;
			if ( $this->checkControllerFormat( $controller ) ){
				$this->isAuthenticated( $controller ) or die( "Controller '". $controller['controller'] ."' with action '". $controller['action'] ."' is not register");
				$objController = $this->loadController( $controller["controller"] );
				call_user_func_array( array( $objController , $controller["action"] ) , $this->request["REQUEST"] );
			}
			else {
				die("ERROR : Controller is not well form.");
			}
		}
		private function validateRoutes(){
			foreach( $this->authenController AS $controller ){
				if ( isset( $controller["controller"] ) && $controller["controller"] != "" && isset( $controller["action"] ) && $controller["action"] != "" ){
					$result = $this->executeQueryAsObject("SELECT * FROM routes WHERE controller='".$controller["controller"]."' AND action='".$controller["action"]."' ;");
					if ( empty( $result ) ){
						$this->executeQuery("INSERT INTO routes SET controller='".$controller["controller"]."', action='".$controller["action"]."' , link='?com=".$controller["controller"]."&action=".$controller["action"]."' , user_type = '0' ;");
					}
				}
			}
			$this->authenController = $this->executeQueryAsArray("SELECT * FROM routes;");
		}
		private function checkControllerFormat( $controller = array() ){
			if ( !empty( $controller ) && isset( $controller["controller"] ) && isset( $controller["action"] ) ){
				return true;
			}
			return false;
		}
		private function isAuthenticated( $controller = array() ){
			foreach ( $this->authenController AS $authController ){
				if ( $controller["controller"] == $authController["controller"] ){
					if( $authController["action"] == $controller["action"] ){
						$user=SESSION::get("user");
						if( ( isset($user) && $user->type == 0 ) ){
							// admin
							return true ;
						}
						elseif( isset($user) && ( $user->type == 1 || $user->type == 2 || $user->type == 3 ) ) {
							// user premium if type = 1 and user free if type = 2
							return true ; 
						}
						elseif( !isset($user) && $authController["user_type"] == 3 ){
							// if user not register then set user can access the free user's page
							return true;
						}
					}
				}
			}
			return false ;
		}
	}
?>